package com.longtop.framework.sys.role.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.longtop.framework.base.BaseService;
import com.longtop.framework.common.Constant;
import com.longtop.framework.common.QueryConditions;
import com.longtop.framework.common.QueryResult;
import com.longtop.framework.security.BaseCheckedException;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.sys.role.model.TSysRole;
import com.longtop.framework.sys.role.model.TSysUserRole;

public class RoleService extends BaseService {

	public static final String BEAN_ID = "roleService";

	/**
	 * 保存、修改、删除一起做！
	 * 
	 * @param changeMap
	 *            对象列表，有三个值，删除主键列表、更新bean的列表、新增bean的列表
	 * @param sessionObject
	 * @return 操作成功受影响的记录数，有三个值，插入成功的记录数、更新成功的记录数、删除成功的记录数
	 * @throws BaseCheckedException
	 */
	public Integer[] saveRole(Map changeMap, SessionObject sessionObject) throws BaseCheckedException {

		// 插入成功的记录数、更新成功的记录数、删除成功的记录数
		int insertCount = 0;
		int updateCount = 0;
		int deleteCount = 0;

		// 删除主键列表、更新bean的列表、新增bean的列表
		List insertBeanList = (ArrayList) changeMap.get(Constant.INSERT_BEAN_LIST);
		List updateBeanList = (ArrayList) changeMap.get(Constant.UPDATE_BEAN_LIST);
		String idList = (String) changeMap.get(Constant.ID_LIST);

		TSysRole role;
		Iterator iterator;

		// 批量插入记录
		if (insertBeanList.size() > 0) {
			iterator = insertBeanList.iterator();
			while (iterator.hasNext()) {
				role = (TSysRole) iterator.next();
				// 校验id是否重复。
				List list = this.queryAll("from TSysRole where roleId='" + role.getRoleId() + "'");
				if (list.size() > 0)
					throw new BaseCheckedException("", "保存失败,该角色【" + role.getRoleId() + "】已经存在!");

				super.save(role);
				insertCount++;
			}
		}

		// 批量更新记录
		if (updateBeanList.size() > 0) {
			iterator = updateBeanList.iterator();
			while (iterator.hasNext()) {
				role = (TSysRole) iterator.next();
				super.update(role);
				updateCount++;
			}
		}

		// 批量删除记录
		if (idList.length() > 0) {
			String sql = "delete from TSysRole where roleId in(" + idList + ")";
			deleteCount = super.batchUpdate(sql);
		}

		return new Integer[] { new Integer(insertCount), new Integer(updateCount), new Integer(deleteCount) };
	}

	/**
	 * 依据条件，读取角色列表
	 * 
	 * @param queryConditions
	 *            查询条件
	 * 
	 * @return
	 */
	public QueryResult loadRole(QueryConditions queryConditions, String filter) {

		String orderBy = "sortNo asc";
		queryConditions.setOrderBy(orderBy);

		return this.loadByConditions(queryConditions, TSysRole.class, filter);
	}

	/**
	 * 保存角色分配多用户
	 * 
	 * @param objectList
	 * @param sessionObject
	 * @return
	 * @throws BaseCheckedException
	 */
	public Integer[] saveUserRole(Map changeMap, SessionObject sessionObject) throws BaseCheckedException {
		// 插入成功的记录数、更新成功的记录数、删除成功的记录数
		int insertCount = 0;
		int updateCount = 0;
		int deleteCount = 0;

		// 删除主键列表、更新bean的列表、新增bean的列表
		List insertBeanList = (ArrayList) changeMap.get(Constant.INSERT_BEAN_LIST);
		List updateBeanList = (ArrayList) changeMap.get(Constant.UPDATE_BEAN_LIST);
		String idList = (String) changeMap.get(Constant.ID_LIST);

		TSysUserRole sysUserRole;
		Iterator iterator;

		// 批量插入记录
		if (insertBeanList.size() > 0) {
			iterator = insertBeanList.iterator();
			while (iterator.hasNext()) {
				sysUserRole = (TSysUserRole) iterator.next();

				// 校验员工是否已经拥有该角色：
				List list = this.queryAll("from TSysUserRole where roleId='" + sysUserRole.getRoleId() + "' and userId='" + sysUserRole.getUserId() + "'");
				if (list.size() > 0) {
					continue;
				}
				super.save(sysUserRole);
				insertCount++;
			}
		}

		// 批量更新记录
		if (updateBeanList.size() > 0) {
			iterator = updateBeanList.iterator();
			while (iterator.hasNext()) {
				sysUserRole = (TSysUserRole) iterator.next();

				// 校验员工是否已经拥有该角色：
				List list = this.queryAll("from TSysUserRole where roleId='" + sysUserRole.getRoleId() + "' and userId='" + sysUserRole.getUserId() + "' and id!='" + sysUserRole.getId() + "'");
				if (list.size() > 0) {
					super.deleteAll(list);// 删除原存在的记录。
					// throw new BaseCheckedException("更新失败，员工【" + sysUserRole.getUserId() + "】已经拥有该角色。");
				}

				super.update(sysUserRole);
				updateCount++;
			}
		}

		// 批量删除记录
		if (idList.length() > 0) {
			String sql = "delete from TSysUserRole where id in(" + idList + ")";
			deleteCount = super.batchUpdate(sql);
		}

		return new Integer[] { new Integer(insertCount), new Integer(updateCount), new Integer(deleteCount) };
	}
}